/**
 * 
 */
package dao.jpa;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import domain.Drone;
import domain.Flotte;

/**
 * @author root
 *
 */
@Transactional
@Repository("droneDao")
public class DroneDaoImpl implements DroneDao{

	@Autowired
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public void createAndSaveDrone(Drone drone) {
		Flotte flotte = drone.getFlotte();
		Set<Drone> drones = new HashSet<Drone>();
		drones.add(drone);
		flotte.setDrones(drones);
		sessionFactory.getCurrentSession().persist(drone);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Drone> getAllDrone() {
		return sessionFactory.getCurrentSession().createCriteria(Drone.class).list();
		
	}

	@Override
	public Drone getDroneById(String id) {
		return (Drone) sessionFactory.getCurrentSession().get(Drone.class, id);

	}

	@Override
	public void deleteDrone(Drone drone) {
		sessionFactory.getCurrentSession().delete(drone);
	}



}
